<?php
	class LoginView
	{
		public function DoLoginBox()
		{
          	$xhtml = "
				<form name='loginFormName' id='loginform' action='". htmlentities($_SERVER['REQUEST_URI']) ."' method='post'>       	
	            	<div>
	          			<p class='loginLabel'>
	          				<span class='basicSmallText'>User:</span>
		            	</p>
	          			<p class='loginInputs'>
		            		<input class='loginTextInput' maxlength='25' type='text' name='username'/>
		            	</p>
	            	</div>
	            	<div>
		            	<p class='loginLabel'>
		            		<span class='basicSmallText'>Pass:</span>
		            	</p>	
		          		<p class='loginInputs'>
		          			<input class='loginTextInput' maxlength='15' type='password' name='password'/>
		          		</p>
		          	</div>
		          	<div>
		          		<p>
		          			<span class='basicSmallText'>Remember (beta)</span>
		                	<input type='checkbox' name='remember'/>
		          		</p>
		          		<p>
		          			<input type='submit' class='standardButton' id='loginButton' name='tryLogin' value='Login'/>
		          		</p>
		          	</div>
          		</form>";
          		return $xhtml;
		}
		
		public function DoLogoutBox($loggedInUser, $lastVisit)
		{   		
    		$xhtml = "
        		<div class='logoutBox'>
        			<form action='" . htmlentities($_SERVER['PHP_SELF']) ."' method='post'>
          				<input class='standardButton' type='submit' id='logoutButton' name='tryLogout' value='Logout'/>
            				<p>
            					Logged in as : 				
	            				<span class='underline'>".
	      							$loggedInUser->GetUserName() . "
	      						</span>
      						</p>
      						<p>";
    	      				if($lastVisit != null)
      						{
      							$xhtml .= "Last visited : <span class='underline'>" . $lastVisit . "</span>";
      						}
      						$xhtml .= "</p>
          			</form>
       			</div>
      		";  
      	return $xhtml;
		}
		
	    // Retrive password, if a cookie is set, this is returned
    public function GetPassword()
    {
      if(isset($_COOKIE["pass"]))
      {
        $cookiePass = $_COOKIE["pass"];
        
        // Decoding
        $decoded = base64_decode($cookiePass); 
        $subDecoded = substr($decoded,3);
        
        
        return $subDecoded;
      }   
      else if(isset($_POST["password"]))
      {
        $password = $_POST["password"];
        return $password;
      }
      else
      {
        return null;
      } 
    }
    
    // If the user tried to login event
    public function TriedToLogIn()
    {
      if(isset($_POST['tryLogin']))
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    
    // Sets a cookie user 1 week
    public function SetACookieUser($value)
    {
      // Encode  
      $newValue = 62*3 . $value;  
      $encodedValue = base64_encode($newValue);
        
      setcookie('user', $encodedValue, time()+604800);
    }
    
    // Sets a cookie password 1 week
    public function SetACookiePass($value)
    {
      // Encode  
      $newValue = 27*5 . $value;  
      $encodedValue = base64_encode($newValue);  
      
      setcookie('pass', $encodedValue, time()+604800);
    }
    
		// If user tried to logout event
		public function TriedToLogout()
		{
			if(isset($_POST['tryLogout']))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
    
		// If "remember me" isset
		public function RememberMe()
		{
			if(isset($_POST['remember']))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}